package com.whiteape.book.core.parser;

import com.whiteape.book.core.manager.ParserManager;
import com.whiteape.book.model.bo.ChapterBo;
import com.whiteape.book.model.dto.CollectionMediaBean;
import com.whiteape.book.model.dto.MediaChapterBean;
import com.whiteape.book.model.dto.SiteSourceBean;
import com.whiteape.book.model.dto.SiteSourceChapterInfoBean;
import com.whiteape.book.utils.MD5Utils;
import com.whiteape.book.utils.URLUtils;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.Single;

public class CatalogParser {

    public static Single<List<MediaChapterBean>> parse(SiteSourceBean siteSourceBean, CollectionMediaBean mediaBean, String html) {
        if (siteSourceBean == null || StringUtils.isEmpty(html)) {
            return Single.create(emitter -> emitter.onError(new Throwable("CatalogParser: check args error")));
        }


        return Single.create(emitter -> {

            List<MediaChapterBean> chapterBoList = parseDirectly(siteSourceBean, mediaBean, html);
            if (chapterBoList.isEmpty()) {
                emitter.onError(new Throwable("no data"));
            }
            emitter.onSuccess(chapterBoList);

        });


    }

    public static List<MediaChapterBean> parseDirectly(SiteSourceBean siteSourceBean, CollectionMediaBean mediaBean, String html) {

        String chapterListUrl = mediaBean.getCatalogUrl();

        SiteSourceChapterInfoBean ruleBean = siteSourceBean.getChapterInfoRule();

        List<MediaChapterBean> chapterBoList = new ArrayList<>();
        String listRule = ruleBean.getList();
        Boolean orderAsc = ruleBean.getOrderAsc();
        orderAsc = orderAsc == null ? true : orderAsc;

        List listElements = ParserManager.getElementList(mediaBean.getCatalogUrl(), html, listRule, 1, mediaBean.getVarMap());
        if (listElements == null || listElements.size() == 0) {
            return chapterBoList;
        }

        //TODO 先不管chapter分页了
        for (Object element : listElements) {
            MediaChapterBean chapterBo = new MediaChapterBean();
            String doc = ParserManager.getElementString(element);
            if (!StringUtils.isEmpty(ruleBean.getUrl())) {
                String url = ParserManager.parseOne(chapterListUrl, doc, ruleBean.getUrl(), mediaBean.getVarMap());
                if (!url.startsWith("http")) {
                    url = URLUtils.getAbsolutePath(chapterListUrl, url);
                }
//                chapterBo.setCatalogUrl(chapterListUrl);

                chapterBo.setLink(url);
                chapterBo.setId(MD5Utils.strToMd5By16(url));
            }
            chapterBo.setName(ParserManager.parseOne(chapterListUrl, doc, ruleBean.getName(), mediaBean.getVarMap()));
            if ("".equals(chapterBo.getLink()) || "".equals(chapterBo.getName())) {
                continue;
            }

            chapterBo.setMediaId(mediaBean.getId());
            chapterBoList.add(chapterBo);
        }
        if (StringUtils.isEmpty(ruleBean.getUrl())) {
            String content = ParserManager.parseOne(chapterListUrl, html, ruleBean.getBatchUrl(), mediaBean.getVarMap());
            String[] urls = content.split(",");
            if(urls.length != chapterBoList.size()) {
                return new ArrayList<>();
            }
            for (int i = 0; i <urls.length; i++) {
                chapterBoList.get(i).setLink(urls[i]);
                chapterBoList.get(i).setId(MD5Utils.strToMd5By16(urls[i]));
            }

        }

        if (!orderAsc) {
            Collections.reverse(chapterBoList);
        }

        return chapterBoList;


    }
}
